<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <input type="text" id="content" />
    <input type="button" value="发送" id="send" />
    <input type="button" value="关闭" id="close" />
</body>
<script type="text/javascript">
    /* 封装 WebSocket 实例化的方法  */
    var CreateWebSocket = (function () {
        return function (urlValue) {
            if(window.WebSocket) return new WebSocket(urlValue);
            if(window.MozWebSocket) return new MozWebSocket(urlValue);
            return false;
        }
    })();
    /* 实例化 WebSocket 连接对象, 地址为 ws 协议 */
    var webSocket = CreateWebSocket("ws://192.168.1.203:60110");
    /* 接收到服务端的消息时 */
    webSocket.onmessage = function (msg) {
        console.log("服务端说:" + msg.data);
    };
    /* 关闭时 */
    webSocket.onclose = function () {
        console.log("关闭连接");
    };
    /* 发送消息 */
    document.getElementById("send").onclick = function () {
        var str = document.getElementById("content").value;
        webSocket.send(str);
    }
    /* 关闭消息 */
    document.getElementById("close").addEventListener("click", function () {
        webSocket.close();
    });
</script>
</html>